package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.io.*;
import java.io.File;
import java.io.FileOutputStream;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by heyecheng on 18/2/5.
 */
@RestController
public class MergeImgAPI {
    @Value("${file.merge.baseimg}")
    private String baseimgpath;

    @Value("${file.merge.mergepath}")
    private String mergepath;

    @RequestMapping(value="/showimg")
    public ResponseBean mergeImg(HttpServletRequest request) throws Exception {
        String name = request.getParameter("name");
        Double time = Double.valueOf(request.getParameter("time"));

        String[] filePath = {baseimgpath + "danshengou.jpg",
                baseimgpath + "hashiqi.jpg",
                baseimgpath + "jinmao.jpg",
                baseimgpath + "jiwawa.jpg",
                baseimgpath + "bomei.jpg",
                baseimgpath + "taidi.jpg"
        };

        int res = 0;
        if (time < 3){
            res = 0;
        }else if (time < 4){
            res = (int)(Math.random()*2) + 1;
        }else {
            res = (int)(Math.random()*3) + 3;
        }

        try {
            BufferedImage outImg = ImageIO.read(new File(filePath[res]));//读取本地图片
            Graphics g = outImg.getGraphics();//开启画图

            g.setColor(new Color(253, 88 ,29));

            if (StringUtils.isNotBlank(name)){
                int length = name.length();
                if (length <= 3){
                    g.setFont(new Font("", Font.PLAIN, 35 ));
                    g.drawString(name, 250, 475);//绘制文字
                }else if (length == 4){
                    g.setFont(new Font("", Font.PLAIN, 35 ));
                    g.drawString(name, 225, 475);//绘制文字
                }
                else if (length == 5){
                    g.setFont(new Font("", Font.PLAIN, 30 ));
                    g.drawString(name, 220, 473);//绘制文字
                }else {
                    g.setFont(new Font("", Font.PLAIN, 25 ));
                    g.drawString(name.substring(0, 5), 225, 455);//绘制文字
                    g.drawString(name.substring(5), 210, 485);//绘制文字
                }
            }


            g.setFont(new Font("", Font.PLAIN, 35 ));
            g.drawString(time + "s", 415, 475);//绘制文字
            g.drawString("95%", 395, 530);

            Image headpic = ImageIO.read(new File(baseimgpath + "2.jpeg"));

            g.drawImage(convertCircular(ImageIO.read(new File(baseimgpath + "2.jpeg"))).getScaledInstance(200, 200, Image.SCALE_DEFAULT), 343, 175, null);
            g.drawImage(headpic.getScaledInstance(170, 170, Image.SCALE_DEFAULT), 550, 1040, null);


            g.dispose(); // 释放绘图资源

            Random random = new Random();
            String fileName = random.nextInt(1000) + ".jpg";
            ImageIO.write(outImg, "jpg", new File(mergepath + fileName));
            return ResponseBean.ok("https://wx.yangbingtech.com/material/mergepath/" + fileName);
        }catch (Exception e){
            return ResponseBean.fail("图片生成失败");
        }
    }

    public static BufferedImage convertCircular(BufferedImage bi1) throws IOException{
        //这种是黑色底的
        BufferedImage bi2 = new BufferedImage(bi1.getWidth(),bi1.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);

        Ellipse2D.Double shape = new Ellipse2D.Double(0,0,bi1.getWidth(),bi1.getHeight());
        Graphics2D g2 = bi2.createGraphics();
        g2.setClip(shape);
        // 使用 setRenderingHint 设置抗锯齿
        g2.drawImage(bi1,0,0,null);
        //设置颜色
        g2.setBackground(Color.green);
        g2.dispose();
        return bi2;
    }

}
